<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单编号" prop="orderCode">
      <el-input v-model="dataForm.orderCode" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="平台id" prop="platformId">
      <el-input v-model="dataForm.platformId" placeholder="平台id"></el-input>
    </el-form-item>
    <el-form-item label="仓库id" prop="wareId">
      <el-input v-model="dataForm.wareId" placeholder="仓库id"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="状态(1:已付款2:代付款3:已取消4:已发货5：订单已过期)" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态(1:已付款2:代付款3:已取消4:已发货5：订单已过期)"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="price">
      <el-input v-model="dataForm.price" placeholder="价格"></el-input>
    </el-form-item>
    <!-- <el-form-item label="版本" prop="version">
      <el-input v-model="dataForm.version" placeholder="版本"></el-input>
    </el-form-item>
    <el-form-item label="支付时间" prop="paytime">
      <el-input v-model="dataForm.paytime" placeholder="支付时间"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createtime">
      <el-input v-model="dataForm.createtime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="修改时间" prop="modifytime">
      <el-input v-model="dataForm.modifytime" placeholder="修改时间"></el-input>
    </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          orderCode: '',
          userId: '',
          platformId: '',
          wareId: '',
          remark: '',
          status: '',
          price: '',
          version: '',
          paytime: '',
          createtime: '',
          modifytime: ''
        },
        dataRule: {
          orderCode: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          platformId: [
            { required: true, message: '平台id不能为空', trigger: 'blur' }
          ],
          wareId: [
            { required: true, message: '仓库id不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态(1:已付款2:代付款3:已取消4:已发货5：订单已过期)不能为空', trigger: 'blur' }
          ],
          price: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          version: [
            { required: true, message: '版本不能为空', trigger: 'blur' }
          ],
          paytime: [
            { required: true, message: '支付时间不能为空', trigger: 'blur' }
          ],
          createtime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          modifytime: [
            { required: true, message: '修改时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/order/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderCode = data.order.orderCode
                this.dataForm.userId = data.order.userId
                this.dataForm.platformId = data.order.platformId
                this.dataForm.wareId = data.order.wareId
                this.dataForm.remark = data.order.remark
                this.dataForm.status = data.order.status
                this.dataForm.price = data.order.price
                this.dataForm.version = data.order.version
                this.dataForm.paytime = data.order.paytime
                this.dataForm.createtime = data.order.createtime
                this.dataForm.modifytime = data.order.modifytime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/order/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'orderCode': this.dataForm.orderCode,
                'userId': this.dataForm.userId,
                'platformId': this.dataForm.platformId,
                'wareId': this.dataForm.wareId,
                'remark': this.dataForm.remark,
                'status': this.dataForm.status,
                'price': this.dataForm.price,
                'version': this.dataForm.version,
                'paytime': this.dataForm.paytime,
                'createtime': this.dataForm.createtime,
                'modifytime': this.dataForm.modifytime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
